package czh.cocoa.ui.impl;

import java.util.Date;

import czh.cocoa.foundation.NSCalendar;
import czh.cocoa.foundation.NSDate;
import czh.cocoa.foundation.NSDateComponents;
import czh.cocoa.foundation.NSString;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.ui.UIFont;

import static czh.cocoa.foundation.NSCalendar.*;

public class UIDatePicker extends UIPickerView implements UIPickerViewDataSource, UIPickerViewDelegate {

	public final static int UIDatePickerModeTime = 0;
	public final static int UIDatePickerModeDate = 1;
	public final static int UIDatePickerModeDateAndTime = 2;
	public final static int UIDatePickerModeCountDownTimer = 3;
	
	private int mDatePickerMode;
	
	private NSDate mDate;
	
	private double mCountDownDuration;
	
	private UILabel mHourLabel;
	
	private UILabel mMinuteLabel;
	
	public UIDatePicker() {
		
	}
	
	public UIDatePicker(CGRect frame) {
		super(frame);
	}
	
	protected void init() {
		super.init();
		setDataSource(this);
		setDelegate(this);
		mDatePickerMode = UIDatePickerModeTime;
	}
	
	public void setDatePickerMode(int mode) {
		mDatePickerMode = mode;
	}
	
	public int datePickerMode() {
		return mDatePickerMode;
	}
	
	public void reloadAllComponents(){
		super.reloadAllComponents();
		
		if (mHourLabel != null) {
			mHourLabel.removeFromSuperview();
			mHourLabel = null;
		}
		
		if (mMinuteLabel != null) {
			mMinuteLabel.removeFromSuperview();
			mMinuteLabel = null;
		}
		
		final int mode = mDatePickerMode;
		if (mode == UIDatePickerModeTime) { 
			if (mDate == null) {
				mDate = new NSDate();
			}
			setDate(mDate);
		} else if (mode == UIDatePickerModeCountDownTimer) {
			CGRect bounds = bounds();
			
			UILabel hour_label = new UILabel(CGRectMake(0, 0, 100, 100)); 
			hour_label.setFont(UIFont.boldSystemFontOfSize(20)); 
			hour_label.setText(NSTR("小时")); 
			hour_label.sizeToFit();
			hour_label.setCenter(CGPointMake(bounds.size.width / 4, bounds.size.height / 2)); 
			addSubview(hour_label);
			
			UILabel minute_label = new UILabel(CGRectMake(0, 0, 100, 100)); 
			minute_label.setFont(UIFont.boldSystemFontOfSize(20)); 
			minute_label.setText(NSTR("分钟")); 
			minute_label.sizeToFit();
			minute_label.setCenter(CGPointMake(bounds.size.width * 3 / 4, bounds.size.height / 2)); 
			addSubview(minute_label);
			
			mHourLabel = hour_label;
			mMinuteLabel = minute_label;
			
			setCountDownDuration(mCountDownDuration);
		}
	}
	
	public void setDate(NSDate date) {
		mDate = date;
		final int mode = mDatePickerMode;
		if (mode == UIDatePickerModeTime) { 
			NSCalendar calendar = NSCalendar.currentCalendar();
			int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit;
			NSDateComponents dc = calendar.componentsFromDate(unitFlags, mDate);
			
			int hour = dc.hour();
			if (hour < 12) {
				selectRowInComponentAnimated(0, 0, false); 
				if (hour == 0) {
					selectRowInComponentAnimated(11, 1, false); 
				} else {
					selectRowInComponentAnimated(hour - 1, 1, false);
				}
			} else {
				selectRowInComponentAnimated(1, 0, false); 
				if (hour == 12) {
					selectRowInComponentAnimated(11, 1, false); 
				} else {
					selectRowInComponentAnimated(hour - 13, 1, false);
				}
			}
			selectRowInComponentAnimated(dc.minute(), 2, false); 
		}
	}
	
	public NSDate date() {
		return mDate;
	}
	
	public void setCountDownDuration(double duration) {
		mCountDownDuration = duration;
		if (mDatePickerMode == UIDatePickerModeCountDownTimer) { 
			int hour = Math.min(23, (int) (duration / (60 * 60)));
			int minute = (int)((duration % (60 * 60)) / 60);
			selectRowInComponentAnimated(hour, 0, false);
			selectRowInComponentAnimated(minute, 1, false);
		}
	}
	
	public double countDownDuration() {
		return mCountDownDuration;
	}

	@Override
	public int numberOfComponentsInPickerView(UIPickerView pickerView) {
		final int mode = mDatePickerMode;
		if (mode == UIDatePickerModeTime) { 
			return 3; 
		} else if (mode == UIDatePickerModeCountDownTimer) {
			return 2;
		}
		return 0;
	}

	@Override
	public int pickerViewNumberOfRowsInComponent(UIPickerView pickerView, int component) {
		final int mode = mDatePickerMode;
		if (mode == UIDatePickerModeTime) { 
			if (component == 0) {
				return 2;
			} else if (component == 1) {
				return 12;
			} else if (component == 2) {
				return 60;
			}
		} else if (mode == UIDatePickerModeCountDownTimer) {
			if (component == 0) {
				return 24;
			} else if (component == 1) {
				return 60;
			}
		}
		return 0;
	}
	
	@Override
	public NSString pickerViewTitleForRowForComponent(UIPickerView pickerView, int row, int component) {
		final int mode = mDatePickerMode;
		if (mode == UIDatePickerModeTime) { 
			if (component == 0) {
				return row == 0 ? NSTR("上午") : NSTR("下午");
			} else if (component == 1) {
				return NSTR("" + (row + 1));
			} else if (component == 2) {
				return row < 10 ? NSTR("0" + row) : NSTR("" + row); 
			}
		} else if (mode == UIDatePickerModeCountDownTimer) {
			return NSTR("" + row);
		}
		return null;
	}
	
	@Override
	public void pickerViewDidSelectRowInComponent(UIPickerView pickerView, int row, int component) {
		final int mode = mDatePickerMode; 
		if (mode == UIDatePickerModeTime) { 
			NSCalendar calendar = NSCalendar.currentCalendar();
			int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit;
			NSDateComponents dc = calendar.componentsFromDate(unitFlags, mDate);
			int hour = dc.hour();
			if (component == 0) { 
				if (row == 0) {
					if (hour >= 12) {
						dc.setHour(hour - 12);
					}
				} else {
					if (hour < 12) {
						dc.setHour(hour + 12);
					}
				}
			} else if (component == 1) {
				if (hour >= 12) {
					dc.setHour(row == 11 ? 12 : row + 12);
				} else {
					dc.setHour(row == 11 ? 0 : row);
				}
			} else {
				dc.setMinute(row);
			}
			mDate = calendar.dateFromComponents(dc);
			
		} else if (mode == UIDatePickerModeCountDownTimer) {
			if (component == 0) {
				int minute = (int)((mCountDownDuration % (60 * 60)) / 60);
				mCountDownDuration = row * 60 * 60 + minute * 60;
			} else {
				int hour = Math.min(23, (int) (mCountDownDuration / (60 * 60)));
				mCountDownDuration = hour * 60 * 60 + row * 60;
			}
		}
	}

}
